// pages/wallet/cash.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cash: '',
    showCard:false
  },
  bindcard:function(){
    wx.navigateTo({
      url: '/pages/wallet/bindcard',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getWallet()
    this.getCardList()
  },
  // 获取钱包信息
  getWallet: function () {
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/money?wxopenid=' + wx.getStorageSync('openId'),
      success: function (res) {
        console.log(res)
        var total_money = res.data.total_money
        if (total_money == '没有零钱') {
          total_money = '0.00'
        } else {
          total_money = Number(total_money).toFixed(2)
        }
        that.setData({
          total_money: total_money
        })
      }
    })
  },
  // 银行卡列表
  getCardList:function(){
    var that = this
    wx.request({
      url: app.globalData.url + 'Card/seebank?wxopenid=' + wx.getStorageSync('openId'),
      success:function(res){
        console.log(res)
        var bank = res.data.bank
        for (var i = 0; i < bank.length; i++){
          bank[i].bank_card_str = bank[i].bank_card.substr(-4,4)
        }
       
        console.log(bank)
        that.setData({
          bank: bank,
          selectedBank: bank[0],
        })
      }
    })
  },
  // 全部提现金额
  cashAll:function(){
    var total_money = this.data.total_money
    this.setData({
      cash: total_money
    })
  },
  // 提现金额
  getCash:function(e){
    var cash = e.detail.value
    this.setData({
      cash: cash
    })
  },
  // 显示选择器
  showCardpicker: function () {
    this.setData({
      showCard: !this.data.showCard
    })
  },
  // 关闭
  close: function () {
    this.setData({
      showCard: false, success: false,
    })
  },
  // 选择银行卡
  selectBank:function(e){
    var bank = this.data.bank
    var o = e.currentTarget.dataset
    this.setData({
      bankIndex: o.index,
      selectedBank: bank[o.index]
    })
  },
  // 提现申请
  cash:function(){
    var that= this
    wx.request({
      url: app.globalData.url+ 'Card/draw',
      data:{
        wxopenid:wx.getStorageSync('openId'),
        money: this.data.cash,
        bank_id:this.data.selectedBank.id
      },
      success:function(res){
        console.log(res)
        if(res.data.code == 0){
          that.getWallet()
        }
        that.setData({
          success: true,
          msg: res.data.msg
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})